package com.teapos.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.teapos.service.PadService;
import com.teapos.service.ScatService;
import com.teapos.util.Maps;
import com.teapos.util.Tool;

@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/pad")
@Controller
public class PadController extends BaseController {
	
	@Autowired
	private PadService padService;
	@Autowired
	private ScatService qRcodeService;
	
	@RequestMapping("/index")
	public String index(HttpServletRequest req, Map map) {
		List stores = super.getUserStores(req);
		Map param = super.getParaMap(req);
		Tool.initPage(param);
		if(stores.size()==1) param.put("storeId", ((Map)stores.get(0)).get("store_id"));
		map.put("orders", padService.getPadOrders(param));
		map.put("page", Maps.newHashMap("pageNo", Tool.initPageNo(req.getParameter("pageNo")), "total",
				padService.getPadOrderCount(param), "js", "turnpage"));
		return "pad/index";
	}
	
	@RequestMapping(value="/pay", method=RequestMethod.GET)
	public String jump_pay() {
		return "pad/pay";
	}
	
	@ResponseBody
	@RequestMapping(value="/pay", method=RequestMethod.POST)
	public String pay(HttpServletRequest req) {
		try {
			padService.checkAndSaveOrder(super.getParaMap(req));
			return success("下单成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("下单失败！！");
		}
	}
	
	@RequestMapping("/detail")
	public String detail(String print,int id, Map map) {
		map.put("detail", padService.getPadOrderDetail(id));
		if( "1".equals(print) ) {
			return "print/print_pad_detail";
		} else {
			return "pad/detail";
		}
	}
	
	@RequestMapping("/prepare")
	public String prepare(int id, Map map) {
		map.put("items", padService.getPadOrderItems(id));
		map.put("orderId", id);
		System.err.println(map);
		return "pad/prepare";
	}
	
	@ResponseBody
	@RequestMapping("/scan")
	public String scan(HttpServletRequest req, HttpServletResponse res, String code) {
		String oldcodes = Tool.getCookieValue(req, "codes");
		if(StringUtils.isBlank(oldcodes)||oldcodes.indexOf(code)==-1) {
			Map qrcode = qRcodeService.findQRcode(code);
			res.addCookie(new Cookie("codes", Tool.toString(oldcodes)+"&"+qrcode.get("code")));
			return success(null, qrcode);
		} else {
			return fail("请勿重复扫描！！");
		}
	}
	
	@ResponseBody
	@RequestMapping("/send")
	public String send(HttpServletRequest req, int orderId) {
		String value = Tool.getCookieValue(req, "pad_products");
		List products = JSON.parseArray(value);
		System.err.println(products);
		Map result = padService.checkAndSaveOrderItem(products, orderId);
		if((Boolean)result.get("isSuccess")) return success("发货成功！！");
		else return fail(Tool.toString(result.get("msg")));
	}

}
